package com.example.android_demo_3_graph;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.LegendAlign;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
import com.jjoe64.graphview.LineGraphView;

public class MainActivity extends Activity {

	Button btnLine;
	Button btnAddData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnLine = (Button)this.findViewById(R.id.btnLine);
		btnLine.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				lineGraph();	
			}
		});		
		
		btnAddData = (Button)this.findViewById(R.id.btnAddData);
		btnAddData.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(exampleSeries != null)
				{
					dataX++;
					dataY += 0.5;
					exampleSeries.appendData(new GraphViewData(dataX, dataY), true);
				}
			}
		});
	}
	
	static int dataX = 4;
	static int dataY = 1;
	GraphViewSeries exampleSeries;
	private void lineGraph()
	{
		exampleSeries = new GraphViewSeries(
				"Day info"
				, new GraphViewSeriesStyle(Color.rgb(90, 250, 00), 3)
				, new GraphViewData[] {
						new GraphViewData(1, 2.0d),
						new GraphViewData(2, 1.5d), 
						new GraphViewData(3, 2.5d),
						new GraphViewData(4, 1.0d) });
		GraphView graphView = new LineGraphView(this // context
				, "Line graph view" // heading
		);
		
		//set scrollable
		graphView.setScrollable(true);
		//customize label
		graphView.setHorizontalLabels(new String[] {"2 days ago", "yesterday", "today", "tomorrow"});
		graphView.setVerticalLabels(new String[] {"high", "middle", "low"});
		
		//customize legend
		graphView.setShowLegend(true);
		graphView.setLegendAlign(LegendAlign.BOTTOM);
		graphView.setLegendWidth(100);
		
		graphView.addSeries(exampleSeries); // data
		LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
		layout.addView(graphView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
